package com.talos.ums.controller.admin.userorganization;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.userorganization.vo.UmsUserOrganizationPageReqVO;
import com.talos.ums.controller.admin.userorganization.vo.UmsUserOrganizationRespVO;
import com.talos.ums.controller.admin.userorganization.vo.UmsUserOrganizationSaveReqVO;
import com.talos.ums.dal.dataobject.userorganization.UmsUserOrganizationDO;
import com.talos.ums.service.userorganization.UmsUserOrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户组织关系表")
@RestController
@RequestMapping("/ums/user-organization")
@Validated
public class UmsUserOrganizationController {

    @Resource
    private UmsUserOrganizationService userOrganizationService;

    @PostMapping("/create")
    @Operation(summary = "创建用户组织关系表")
    @PreAuthorize("@ss.hasPermission('ums:user-organization:create')")
    public CommonResult<Long> createUserOrganization(@Valid @RequestBody UmsUserOrganizationSaveReqVO createReqVO) {
        return success(userOrganizationService.createUserOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户组织关系表")
    @PreAuthorize("@ss.hasPermission('ums:user-organization:update')")
    public CommonResult<Boolean> updateUserOrganization(@Valid @RequestBody UmsUserOrganizationSaveReqVO updateReqVO) {
        userOrganizationService.updateUserOrganization(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户组织关系表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:user-organization:delete')")
    public CommonResult<Boolean> deleteUserOrganization(@RequestParam("id") Long id) {
        userOrganizationService.deleteUserOrganization(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户组织关系表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:user-organization:query')")
    public CommonResult<UmsUserOrganizationRespVO> getUserOrganization(@RequestParam("id") Long id) {
        UmsUserOrganizationDO userOrganization = userOrganizationService.getUserOrganization(id);
        return success(BeanUtils.toBean(userOrganization, UmsUserOrganizationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户组织关系表分页")
    @PreAuthorize("@ss.hasPermission('ums:user-organization:query')")
    public CommonResult<PageResult<UmsUserOrganizationRespVO>> getUserOrganizationPage(@Valid UmsUserOrganizationPageReqVO pageReqVO) {
        PageResult<UmsUserOrganizationDO> pageResult = userOrganizationService.getUserOrganizationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsUserOrganizationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户组织关系表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:user-organization:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserOrganizationExcel(@Valid UmsUserOrganizationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsUserOrganizationDO> list = userOrganizationService.getUserOrganizationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户组织关系表.xls", "数据", UmsUserOrganizationRespVO.class,
                        BeanUtils.toBean(list, UmsUserOrganizationRespVO.class));
    }

}
